class Solution:
    def minLength(self, s: str) -> int:
        p = ['K']
        for v in s:
            if v == 'B' and p[-1] == 'A':
                p.pop(-1)
            elif v == 'D' and p[-1] == 'C':
                p.pop(-1)
            else:
                p.append(v)
        return len(p) - 1
